package exception;

import java.util.Scanner;

public class Person {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws IllegalAgeException{
        if (age>100||age<=0){
//            throw new RuntimeException("年龄不合法");
            throw new IllegalAgeException("年龄不合法");
        }
        this.age = age;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Person p = new Person();
        while (true){
            System.out.println("请输入年龄");
            int a = scanner.nextInt();
        try {
            p.setAge(a);
            System.out.println("此人年龄"+p.getAge()+"岁");
            break;
        } catch (IllegalAgeException e) {
            System.out.println("年龄不合法，请重新输入");
        }
        }
    }
}
